#-*- coding: UTF-8 -*-
#coding=UTF-8
from google.appengine.api import urlfetch
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
import datetime
import logging
from google.appengine.api.memcache import Client

#class DataDO(db.Model):
#    tuan_date = db.DateProperty(required=True)
#    qqshop_tuan = db.Text()
#    qq_tuan = db.Text()
#    paipai_tuan = db.Text()
#        ##持久化存储团购信息
#        datado = DataDO(tuan_date=datetime.date.today(),qq_tuan=qq_tuan_info,
#                        qqshop_tuan=qqshop_tuan_info,paipai_tuan=paipai_tuan_info)
#        datado.put()
#        self.response.headers['Content-Type'] = 'text/plain'
#        self.response.out.write('task done!');


''''定时任务：抓取数据，缓存到DB中'''
class storeDataEveryday(webapp.RequestHandler):
    def get(self):
        dataString = datetime.date.today().strftime( "%y%m%d") #日期
        mc = Client()
        ''''抓取qq.tuan页面.解析其中的div展现段落'''
        for i in range(0, 3):
            try:
                url = "http://tuan.qq.com/"
                result = urlfetch.fetch(url)
                if result.status_code == 200:
                    contents = result.content
                    begin = contents.find('<div class="mainbar tody_buy_group">');
                    end =  contents.find('<div class="detail_today_buy">');
                    qq_tuan_info = contents[begin:end]
                    break
                else:
                    continue
            except urlfetch.Error:
                logging.error('urlfetch.Error http://tuan.qq.com/ Marked!try again')
                continue
        mc.set(key=dataString+'qq_tuan_info', value=qq_tuan_info, time=3600*25)
        
        for i in range(0, 3):
            try:
                url = "http://shop.qq.com/act/tuan/today.json.js"
                result = urlfetch.fetch(url)
                if result.status_code == 200:
                    qqshop_tuan_info = result.content
                    break
                else:
                    continue
            except urlfetch.Error:
                logging.error('urlfetch.Error http://shop.qq.com/act/tuan/today.json.js Marked!try again')
                continue
        mc.set(key=dataString+'qqshop_tuan_info', value=qqshop_tuan_info, time=3600*25)

        for i in range(0, 3):
            try:
                url = "http://ext.paipai.com/tuan/tuanpp"
                result = urlfetch.fetch(url,headers={'Content-Type': 'text/html; charset=gb2312'})
                if result.status_code == 200:
                    paipai_tuan_info = result.content
                    break
                else:
                    continue
            except urlfetch.Error:
                logging.error('urlfetch.Error http://ext.paipai.com/tuan/tuanpp Marked!try again')
                continue
        mc.set(key=dataString+'paipai_tuan_info', value=paipai_tuan_info, time=3600*25)    
        

        
        
application = webapp.WSGIApplication(
                                     [('/tasks/', storeDataEveryday)],
                                     debug=True)
def main():
    run_wsgi_app(application)

if __name__ == "__main__":
    main()